<!--
 * @Author: Aaron Huang
 * @Date: 2022-10-19 14:04:40
 * @LastEditors: Aaron Huang
 * @LastEditTime: 2022-11-16 14:14:36
 * @Description: file content
 * @FilePath: \ias_ui\src\views\coi\CBEC\components\addList.vue
 * @Company: 沃尔玛
-->
<template>
  <essTable
    v-loading="loading"
    :table-columns="tableColumns"
    :table-data="tableData"
    :total="total"
    :page-params="pageParams"
    @pageParamsChange="queryTableDataFun"
    @selectionChange="selectionChange"
  >
    <template v-slot:table-header>
      <searchHead
        :search-data="searchData"
        :search-list="searchList"
        :options="options"
        @itemNbrListInput="(value, item) => checkNumberList(value, item, 15, 500)"
        @skuListInput="(value, item) => checkNumberList(value, item, 12, 500)"
        @deptNbrListInput="(value, item) => checkNumberList(value, item, 2, 50)"
        @handleSearch="handleSearch"
        @handleReset="handleReset"
        @handleExport="handleExport"
      />
      <div class="btn">
        <el-button type="primary" size="mini" icon="el-icon-plus" round @click="dialogVisible = true">新增建品</el-button>
        <el-button type="primary" size="mini" icon="el-icon-refresh" round :disabled="hasRePush" @click="open('确定重新推送选中数据？', rePush)">
          重新推送
        </el-button>
      </div>
    </template>
    <template v-slot:sku="{ scope: { row } }">
      <span class="el-button--text el-button--mini" @click="goDetail(row.id, 'detail', row.skuName)">{{ row.sku }}</span>
    </template>
    <template v-slot:operation="{ scope: { row } }">
      <el-button v-if="editBtnShow(row)" type="text" size="mini" @click="goDetail(row.id, 'add', row.skuName)">编辑</el-button>
      <el-button type="text" size="mini" @click="operationLog(row.id)">日志</el-button>
    </template>

    <template v-slot:container>
      <selectBuildingTypeDialog v-model="dialogVisible" :building-type-form="buildingTypeForm" @submitBuildingTypeForm="submitBuildingTypeForm" />
    </template>
    <template v-slot:table-footer>
      <operation-log ref="operationLogRef" :process-data-list="processDataList" />
    </template>
  </essTable>
</template>

<script>
import essTable from '@/components/newTable/index.vue'
import searchHead from '@/components/searchHead/newSearch.vue'
import operationLog from '@/views/coi/approve/components/operationLog.vue'
import { goodsTypeCode, channelCode } from '@/views/coi/commonJs/dictionary'
import { statusCode } from '../js/dictionary'
import selectBuildingTypeDialog from './selectBuildingTypeDialog.vue'
import { tableColumns, searchList, options } from '../js/addListPageParams'
import { initializeData, searchHeaderEvent, initializeMethod, checkBoxFun } from '@/components/newTable/index'
import { tableMixins } from '../js/tableMixins'
import { processData } from '@/api/coi/onlineTaskAPI/sku'
import eventBus from '@/utils/eventBus'

import {
  newList_CBEC_API as queryTableDataApi,
  newListExportFile_CBEC_API as exportTableDataApi,
  newBuilding_CBEC_API,
  rePush_CBEC_API as rePushApi
} from '@/api/coi/CBEC/newProducts'
import { checkNumberList } from '@/components/searchHead/js/checkInput.js'

export default {
  name: 'AddList',
  components: {
    essTable,
    searchHead,
    selectBuildingTypeDialog,
    operationLog
  },
  mixins: [initializeData, searchHeaderEvent, initializeMethod, checkBoxFun, checkNumberList, tableMixins],
  data() {
    return {
      tableColumns,
      queryTableDataApi,
      exportTableDataApi,
      rePushApi,
      searchList,
      options,
      exportFileName: 'SKU_CBEC_创建',
      dialogVisible: false,
      buildingTypeForm: {
        goodsType: goodsTypeCode.CBEC_DROP,
        channelCodes: [channelCode.WMDJ]
      },
      statusCode,
      processDataList: []
    }
  },
  computed: {
    hasRePush() {
      // 列表的状态值是string 枚举是number
      // eslint-disable-next-line
      return !(this.multipleSelection?.length && this.multipleSelection?.every((item) => item.mainStatus == statusCode.PUSH_FAILED))
    }
  },
  methods: {
    editBtnShow({ mainStatus }) {
      return [statusCode.DRAFT, statusCode.PUSH_FAILED].includes(Number(mainStatus))
    },
    async submitBuildingTypeForm() {
      const { code, result } = await newBuilding_CBEC_API(this.buildingTypeForm)
      if (code !== '0') return
      this.dialogVisible = false
      if (typeof result === 'number') this.goDetail(result, 'add')
    },
    goDetail(processId, type, skuName = '') {
      eventBus.$emit('setTagName', skuName)
      this.$router.push({ path: `/CBEC/add/${processId}/${type}/${this.$route.name}/newProducts` })
    },
    // 操作日志
    async operationLog(id) {
      const { result } = await processData(id)
      if (Array.isArray(result) && result.length) {
        this.$refs.operationLogRef.dialogVisible = true
        this.processDataList = result
      } else {
        this.$message({
          type: 'warning',
          message: '未查询到该商品线上建品的操作日志'
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@import url('../css/list.scss');
</style>
